﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using tpos_custom_control;
using app_common;
using tpos_front_end;
using System.Collections;

namespace tpos
{
    public partial class frm_login : Form
    {
        private uc_textbox uc_txt_current;
        
        public frm_login()
        {
            InitializeComponent();
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frm_login_Resize(object sender, EventArgs e)
        {
            gbx_login.Left = this.Width / 2 - gbx_login.Width / 2;
            gbx_login.Top = this.Height / 2 - gbx_login.Height / 2;
        }

        private void uc_txt_user_name_Enter(object sender, EventArgs e)
        {
            uc_txt_current = uc_txt_user_name;
        }

        private void uc_txt_password_Enter(object sender, EventArgs e)
        {
            uc_txt_current = uc_txt_password;
        }

        private void btn_0_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{0}");
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{1}");
        }

        private void btn_2_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{2}");
        }

        private void btn_3_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{3}");
        }

        private void btn_4_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{4}");
        }

        private void btn_5_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{5}");
        }

        private void btn_6_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{6}");
        }

        private void btn_7_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{7}");
        }

        private void btn_8_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{8}");
        }

        private void btn_9_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            SendKeys.Send("{9}");
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            uc_txt_current.uc_Forcus = true;
            uc_txt_current.uc_Text = "";
        }

        private void frm_login_Load(object sender, EventArgs e)
        {

            Hashtable hashResult;
            hashResult = APP_CONNECTION.ReadDbConfig();
            if (hashResult[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.SUCCESS)
            {
                hashResult = APP_CONNECTION.ConnectDatabase();
                if (hashResult[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.ERROR)
                {
                    MessageBox.Show(hashResult[STATUS_MESSAGE.VALUE].ToString(), STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show(hashResult[STATUS_MESSAGE.VALUE].ToString(), STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (uc_txt_user_name.uc_Text == string.Empty) {
                uc_txt_user_name.uc_Forcus = true;
                return;
            }

            if (uc_txt_password.uc_Text == string.Empty) {
                uc_txt_password.uc_Forcus = true;
                return;
            }

            string sql = "SELECT \"user_name\", \"password\", \"full_name\", \"department_code\", \"department_name\", " +
                        "       \"company_code\", \"company_name\", \"is_admin\"" +
                        "FROM \"user\"";

            Hashtable hashResult = APP_SQL_COMMAND.ExecuteReader(sql);
            if (hashResult[STATUS_MESSAGE.MESSAGE].ToString() == STATUS_MESSAGE.ERROR)
            {
                MessageBox.Show("Xảy ra lỗi khi truy vấn dữ liệu.\n\rChi tiết: " + hashResult[STATUS_MESSAGE.VALUE].ToString(), STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else {
                DataTable dtResult = (DataTable)hashResult[STATUS_MESSAGE.VALUE];
                if (dtResult.Rows.Count == 0)
                {
                    MessageBox.Show("Không thể đăng nhập, vui lòng thử lại.", STATUS_MESSAGE.CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else { 
                    //Login successfully
                    USER.user_name = dtResult.Rows[0]["user_name"].ToString();
                    USER.user_full_name = dtResult.Rows[0]["full_name"].ToString();
                    USER.department_code = dtResult.Rows[0]["department_code"].ToString();
                    USER.department_name = dtResult.Rows[0]["department_name"].ToString();
                    USER.company_code = dtResult.Rows[0]["company_code"].ToString();
                    USER.company_name = dtResult.Rows[0]["company_name"].ToString();
                    USER.is_admin = dtResult.Rows[0]["is_admin"].ToString();

                    uc_txt_user_name.uc_Text = "";
                    uc_txt_password.uc_Text = "";

                    this.Hide();
                    frm_main_menu frm_show = new frm_main_menu();
                    frm_show.Show();
                    APP_FORM.frm_login = this;
                }
            }
        }

        private void frm_login_Activated(object sender, EventArgs e)
        {
            uc_txt_user_name.uc_Forcus = true;
        }

        private void frm_login_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F10: btn_ok_Click(sender, e); break;
                case Keys.F11: btn_cancel_Click(sender, e); break;
                case Keys.F12: btn_close_Click(sender, e); break;
            }
        }
    }
}
